package xyz.o0o0o.demo.luajavaDemo;

import java.io.FileNotFoundException;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.PackageLib;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform;

/**
 * Hello world!
 *
 */
public class App {

	static String luaPath1 = "/Users/apple/Documents/helloWorld.lua";
	static String luaPath2 = "/Users/apple/Documents/play.lua";

	public static void main(String[] args) throws FileNotFoundException {
		// testHelloWorld();
		// testPlay();
		// scanDir();
		// testLuaLib();
		// testPlay2();
		testRequire();
	}

	public static void testHelloWorld() {
		Globals globals = JsePlatform.standardGlobals();
		globals.loadfile(luaPath1).call();
		LuaValue result = globals.get("max").call(LuaValue.valueOf(5), LuaValue.valueOf(105));
		System.out.println(result.arg1());
	}

	public static void testPlay() {
		Globals globals = JsePlatform.standardGlobals();
		globals.loadfile(luaPath2).call();

		LuaValue[] values = new LuaValue[4];
		values[0] = CoerceJavaToLua.coerce('p');
		values[1] = CoerceJavaToLua.coerce('2');
		values[2] = CoerceJavaToLua.coerce("30#*#3#*#31#*#3#3#*#0#31#*#3#3#B1");
		values[3] = CoerceJavaToLua.coerce("000003000101030001000*030303");

		Varargs result = globals.invokemethod("play_query", values);
		System.out.println(result.arg1());
	}

	public static void testPlay2() {
		Globals globals = JsePlatform.standardGlobals();
		java.io.File file = new java.io.File("/Users/apple/Documents/plua");
		java.io.File[] list = file.listFiles();
		PackageLib packageLib = new PackageLib();
		packageLib.call(LuaValue.valueOf("plua"), globals);
		packageLib.setLuaPath(luaPath1);
		// packageLib.setLuaPath("/Users/apple/Documents/plua");
		for (java.io.File file2 : list) {
			System.out.println(file2.getPath());
			packageLib.setLuaPath(file2.getPath());
		}
		globals.load(packageLib);

		LuaValue[] values = new LuaValue[4];
		values[0] = CoerceJavaToLua.coerce('p');
		values[1] = CoerceJavaToLua.coerce('2');
		values[2] = CoerceJavaToLua.coerce("30#*#3#*#31#*#3#3#*#0#31#*#3#3#B1");
		values[3] = CoerceJavaToLua.coerce("000003000101030001000*030303");

		LuaValue result = globals.get("max").call(LuaValue.valueOf(5), LuaValue.valueOf(105));
		System.out.println(result.arg1());

		// Varargs result = globals.invokemethod("play_query", values);
		System.out.println(result.arg1());
	}

	// public static void scanDir() {
	// File file = new File("/Users/apple/Documents/plua");
	// File[] list = file.listFiles();
	//
	// for (File file2 : list) {
	// System.out.println(file2.getPath());
	// }
	// }

	public static void testLuaLib() {
		// Globals globals = new Globals();
		// globals.load(new JseBaseLib());
		// PackageLib packageLib = new PackageLib();
		//
		// packageLib.load(library );
		// globals.load();
		//
		//
		// System.out.println(123);
	}

	public static void testRequire() {
		Globals globals = JsePlatform.standardGlobals();

		LuaValue chunk = globals.loadfile("sample-that-imports.lua");
		chunk.call();

		// We could even use our imported library here
		chunk = globals.load("my_imported.printHello()");
		chunk.call();
	}
	
	
}
